/*** This figure plots spending changes and pre-COVID spending shares by industry.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Compute spending declines from published data
*-------------------------------------------------------------------------------

* Load National Affinity data
project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delimited "${root}/data/web/data/Affinity - National - Daily.csv", clear

* Keep data between March 25 and April 14 2020
gen date = mdy(month, day, year)
format %td date
keep if inrange(date, mdy(3, 25, 2020), mdy(4, 14, 2020))

* Average over dates in selected period
gisid date
gcollapse (mean) spend_durables spend_nondurables spend_remoteservices spend_inpersonmisc spend_aer spend_hcs spend_tws spend_acf

* Reshape long
gen temp = 1
greshape long spend_, i(temp) j(category) string
drop temp
rename spend_ spend_decline_covid

* Output
tempfile decline
save `decline'

*-------------------------------------------------------------------------------
* Calculate spending shares
*-------------------------------------------------------------------------------

* Load spending shares
project, uses("${root}/data/web/data/Affinity Industry Composition - National - 2020.csv")
import delimited using "${root}/data/web/data/Affinity Industry Composition - National - 2020.csv", clear
keep industry share_jan2020

* Convert categories to string
rename industry category
replace category = lower(category)
order category

* Merge in declines
merge 1:1 category using `decline', nogen assert(3)

* Compute share of declines
gen weighted_decline = share_jan2020 * spend_decline_covid
gegen total_spend_decline_covid = sum(weighted_decline)
gen share_decline = weighted_decline/total_spend_decline_covid

keep category share_jan2020 share_decline

*-------------------------------------------------------------------------------
* Plot chart
*-------------------------------------------------------------------------------

* Specify order and descriptions of each spending category for bar chart
gen byte type = 1 if category == "durables"
replace type = 2 if category == "nondurables"
replace type = 3 if category == "remoteservices"
replace type = 4 if category == "inpersonmisc"
replace type = 5 if category == "aer"
replace type = 6 if category == "hcs"
replace type = 7 if category == "tws"
replace type = 8 if category == "acf"
label define categorytype 1 "Durable Goods" 2 "Non-Durable Goods" 3 "Remote Services" 4 "Other In-Person Services" 5 "Recreation" 6 "Health Care" 7 "Transportation" 8 "Hotels & Food"
label values type categorytype

* Create stacked bar values
gsort -type
gen agg_decline = sum(share_decline)
gen agg_pre = sum(share_jan2020)

gen x_value = 0
gen x_shift = x_value + 0.85

* Get share of decline accounted for by in-person services
gen in_person = 1 if inrange(type, 4, 8)
sum share_decline if in_person == 1
local decline_early : di %2.0f 100 * `r(sum)'

* Get share of pre-period accounted for by in-person services
sum share_jan2020 if in_person == 1
local share_pre : di %2.0f 100 * `r(sum)'

* Create category labels
sort type
set obs `=_N+1'
replace agg_decline = 0 in -1
replace agg_pre = 0 in -1

forvalues r=1/`=_N-1' {
	sum agg_decline in `r'/`=`r'+1'
	local categorylabels `categorylabels' text(`=r(mean)' `=x_value[1]' "`:label categorytype `r''", size(*0.8))

	sum agg_pre in `r'/`=`r'+1'
	local categorylabels `categorylabels' text(`=r(mean)' `=x_shift[1]' "`:label categorytype `r''", size(*0.8))
}

tw  ///
	(bar agg_decline x_value if type == 1, barwidth(.35) col(gs12)) ///
	(bar agg_decline x_value if type == 2, barwidth(.35) col("230 162 90")) ///
	(bar agg_decline x_value if type == 3, barwidth(.35) col("220 100 90")) ///
	(bar agg_decline x_value if type == 4, barwidth(.35) col( "27 172 72")) ///
	(bar agg_decline x_value if type == 5, barwidth(.35) col("26 196 85")) ///
	(bar agg_decline x_value if type == 6, barwidth(.35) col("27 207 141" )) ///
	(bar agg_decline x_value if type == 7, barwidth(.35) col("27 173 207")) ///
	(bar agg_decline x_value if type == 8, barwidth(.35) col("26 120 196")) ///
	(bar agg_pre x_shift if type == 1, barwidth(.35) col(gs12)) ///
	(bar agg_pre x_shift if type == 2, barwidth(.35) col("230 162 90")) ///
	(bar agg_pre x_shift if type == 3, barwidth(.35) col("220 100 90")) ///
	(bar agg_pre x_shift if type == 4, barwidth(.35) col( "27 172 72")) ///
	(bar agg_pre x_shift if type == 5, barwidth(.35) col("26 196 85")) ///
	(bar agg_pre x_shift if type == 6, barwidth(.35) col("27 207 141" )) ///
	(bar agg_pre x_shift if type == 7, barwidth(.35) col("27 173 207")) ///
	(bar agg_pre x_shift if type == 8, barwidth(.35) col("26 120 196")), ///
	ylab(0 "0%" 0.25 "25%" 0.50 "50%" 0.75 "75%" 1 "100%", nogrid) ///
	xlab(-0.25 " " 0 `" "Share of Decline" "(Jan to Mar 25-Apr 14)" "' 0.85 "Share of Pre-COVID Spending" 1.25" ", notick) ///
	xtitle(" ", size(tiny)) ///
	${title_`version'} ///
	text(`=agg_decline[4] / 2' 0.36 "In-Person" "Services (`decline_early'%)", size(small) j(left)) ///
	text(`=agg_decline[4] / 2' 0.265 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", color(gs10) size(*2.23) j(left)) ///
	text(`=agg_pre[4] / 2' 1.19 "In-Person" "Services (`share_pre'%)", size(small) j(left)) ///
	`categorylabels' ///
	text(`=agg_pre[4] / 2' 1.10 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", color(gs10) size(*1.73) j(left)) ///
	legend(off)

graph export "${root}/results/Spending/Spending Changes by Industry Stacked Bar.svg", replace
project, creates("${root}/results/Spending/Spending Changes by Industry Stacked Bar.svg")

* Black and white version for QJE
tw  ///
	(bar agg_decline x_value if type == 1, barwidth(.35) col(gs7)) ///
	(bar agg_decline x_value if type == 2, barwidth(.35) col(gs8)) ///
	(bar agg_decline x_value if type == 3, barwidth(.35) col(gs9)) ///
	(bar agg_decline x_value if type == 4, barwidth(.35) col(gs10)) ///
	(bar agg_decline x_value if type == 5, barwidth(.35) col(gs11)) ///
	(bar agg_decline x_value if type == 6, barwidth(.35) col(gs12)) ///
	(bar agg_decline x_value if type == 7, barwidth(.35) col(gs13)) ///
	(bar agg_decline x_value if type == 8, barwidth(.35) col(gs14)) ///
	(bar agg_pre x_shift if type == 1, barwidth(.35) col(gs7)) ///
	(bar agg_pre x_shift if type == 2, barwidth(.35) col(gs8)) ///
	(bar agg_pre x_shift if type == 3, barwidth(.35) col(gs9)) ///
	(bar agg_pre x_shift if type == 4, barwidth(.35) col(gs10)) ///
	(bar agg_pre x_shift if type == 5, barwidth(.35) col(gs11)) ///
	(bar agg_pre x_shift if type == 6, barwidth(.35) col(gs12)) ///
	(bar agg_pre x_shift if type == 7, barwidth(.35) col(gs13)) ///
	(bar agg_pre x_shift if type == 8, barwidth(.35) col(gs14)), ///
	ylab(0 "0%" 0.25 "25%" 0.50 "50%" 0.75 "75%" 1 "100%", nogrid) ///
	xlab(-0.25 " " 0 `" "Share of Decline" "(Jan to Mar 25-Apr 14)" "' 0.85 "Share of Pre-COVID Spending" 1.25" ", notick) ///
	xtitle(" ", size(tiny)) ///
	${title_`version'} ///
	text(`=agg_decline[4] / 2' 0.36 "In-Person" "Services (`decline_early'%)", size(small) j(left)) ///
	text(`=agg_decline[4] / 2' 0.265 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", color(gs10) size(*2.23) j(left)) ///
	text(`=agg_pre[4] / 2' 1.19 "In-Person" "Services (`share_pre'%)", size(small) j(left)) ///
	`categorylabels' ///
	text(`=agg_pre[4] / 2' 1.10 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", color(gs10) size(*1.73) j(left)) ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_1b.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_1b.svg")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

cap erase "${root}/results/paper numbers/`category'/Spending Changes by Sector.yaml"

yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector.yaml", ///
	key("person_pre") ///
	comment("In-Person Services, Percentage in Share of Pre-COVID Spending") ///
	value(`share_pre')

yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector.yaml", ///
	key("person_decline") ///
	comment("In-Person Services, Percentage in Share of Decline (Jan to Mar 25-Apr 14)") ///
	value(`decline_early')

project, creates("${root}/results/paper numbers/`category'/Spending Changes by Sector.yaml")
